package edu.westga.sketchit2009cs.stringBufferServer;

import edu.westga.sketchit2009cs.gui.GameInterface;
import edu.westga.sketchit2009cs.gui.Sketchit2009csMainFrame;

public class DrawingGrabber implements Runnable{

	private String theDrawing;
	private String[] theArray;
	private DrawingClient theClient;

	public DrawingGrabber() {
		theDrawing = null;
		theArray = null;
		theClient = null;
	}

	public void run() {
		if (!Sketchit2009csMainFrame.isHost) {
			while(true){
				theClient = new DrawingClient();
				try {
					theClient.send("request");
				} catch (Exception et) {
					System.out.println("Error in requesting drawing");
					et.printStackTrace();
				}
				theDrawing = theClient.recieve();
				//System.out.print(theDrawing + " grabbed data");

				if (!theDrawing.equals("")) {
					theArray = theDrawing.split(" ");
					int xOne = Integer.parseInt(theArray[0]);
					int yOne = Integer.parseInt(theArray[1]);
					int xTwo = Integer.parseInt(theArray[2]);
					int yTwo = Integer.parseInt(theArray[3]);
					GameInterface.drawingPane.paint(xOne, yOne, xTwo, yTwo);
				}
			}
		}
	}

}
